package com.assistant.service.impl;

import com.assistant.mapper.CommentStarMapper;
import com.assistant.mapper.MainStarMapper;
import com.assistant.pojo.CommentStar;
import com.assistant.pojo.MainStar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class StarImpl {
    @Autowired
    private CommentStarMapper commentStarMapper;
    @Autowired
    private MainStarMapper mainStarMapper;


    /*
     *主贴新增
     * */
    public int insertStar(MainStar mainStar) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        mainStar.setCreate_time(dateFormat.format(new Date()));
        mainStarMapper.updateByPrimaryKeySelective(mainStar);
        return 200;
    }

    /*
     * 评论新增
     * */
    public int insertComment(CommentStar commentStar) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        commentStar.setCreate_time(dateFormat.format(new Date()));
        commentStarMapper.insertSelective(commentStar);
        Integer commnetNum=mainStarMapper.selectByPrimaryKey(commentStar.getT_id()).getComment_num();
        commnetNum++;
        MainStar mainStar = new MainStar();
        mainStar.setT_id(commentStar.getT_id());
        mainStar.setComment_num(commnetNum);
        mainStarMapper.updateByPrimaryKeySelective(mainStar);
        return 200;
    }

    /*
     * 查看所有主贴
     * */
    public List<MainStar> seeMainStar(Integer status) {
        Example example = new Example(MainStar.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", status);
        List<MainStar> mainStars = mainStarMapper.selectByExample(example);
        return mainStars;
    }

    /*
    * 插入主贴图片
    * */
public int insertImg(String img){
    MainStar mainStar = new MainStar();
    mainStar.setImg(img);
    if (mainStarMapper.insertSelective(mainStar)==1){
        return mainStar.getT_id();
    }else
        return 0;

}

    /*
     * 查看所有评论
     * */
    public List<CommentStar> seeComment(Integer id) {
        Example example = new Example(CommentStar.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("t_id", id);
        example.orderBy("create_time").asc();
        List<CommentStar> commentStars = commentStarMapper.selectByExample(example);
        return commentStars;
    }


    /*
     * 删除主贴 连带删除评论
     * */
    @Transactional
    public int deleteMainStar(Integer id) {
        Example example = new Example(CommentStar.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("t_id", id);
        commentStarMapper.deleteByExample(example);
        return mainStarMapper.deleteByPrimaryKey(id);
    }

    /*
     * 查看评论数
     * */
    public int selectCommentNum(Integer id) {
        Example example = new Example(CommentStar.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("t_id", id);
        return commentStarMapper.selectCountByExample(example);
    }

    /*
     * 查看阅读数
     * */
    public int selectSeeNum(Integer id) {
        Example example = new Example(CommentStar.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("t_id", id);
        return mainStarMapper.selectOneByExample(example).getSee_num();
    }


    /*
     * 更新点赞+1
     * */
    @Transactional
    public ResponseEntity updateNice(int id) {
        MainStar mainStar = new MainStar();
        mainStar.setT_id(id);
        int like_num = mainStarMapper.selectOne(mainStar).getLike_num();
        like_num++;
        mainStar.setLike_num(like_num);
        mainStarMapper.updateByPrimaryKeySelective(mainStar);
        return new ResponseEntity(null, HttpStatus.OK);
    }


    /*
     * 观看+1
     * */
    @Transactional
    public ResponseEntity updateSeeNum(int id) {
        MainStar mainStar = new MainStar();
        mainStar.setT_id(id);
        int see_num = mainStarMapper.selectOne(mainStar).getSee_num();
        see_num++;
        mainStar.setSee_num(see_num);
        mainStarMapper.updateByPrimaryKeySelective(mainStar);
        return new ResponseEntity(null, HttpStatus.OK);
    }

    /*
     * 多文件上传
     * */
    public String upLoadPic(MultipartFile[] files) {
        // 上传简单文件名(原始文件名)
        String name = null;
        String b = "http://mycampus.natapp1.cc";
        StringBuilder srcs = new StringBuilder();
        String property = System.getProperty("user.dir");
        for (int i = 0; i < files.length; i++) {
            String originalFilename = files[i].getOriginalFilename();
            // 存储路径（项目路径 ）
            String path = property + "/src/main/resources/static/img/" + System.currentTimeMillis();
            name = path + originalFilename;
            try {
                // 保存文件（将上传文件写到服务器上指定的文件）
                files[i].transferTo(new File(name));

            } catch (IOException e) {
                e.printStackTrace();
            }
            if (i == files.length-1) {
                srcs.append(b + name.substring(name.lastIndexOf("/") - 4));
                break;
            }
            srcs.append(b + name.substring(name.lastIndexOf("/") - 4));
            srcs.append("--");
        }

        return String.valueOf(srcs);
    }
    /*
    * 根据主贴t_id返回主贴
    * */
    public MainStar seeMainStarById(Integer id) {
        Example example = new Example(MainStar.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("t_id", id);
       MainStar mainStars = mainStarMapper.selectOneByExample(example);
        return mainStars;
    }
}
